package com.snowwolf.baseUtil.datasource;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.snowwolf.baseUtil.converter.BigDecimalToDecimal128Converter;
import com.snowwolf.baseUtil.converter.Decimal128ToBigDecimalConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: topsnowwolf
 * @description:
 * @date: Create in 2019/3/14 22:08
 * @modified by:
 * @versions：0.1.0
 */
@Configuration
public class MongodbDataSourceConfig extends AbstractMongoConfiguration {
    @Value("${spring.data.mongodb.uri}")
    private String url;
    @Value("${spring.data.mongodb.password}")
    private String pwd;
    @Value("${spring.data.mongodb.database}")
    private String dbName;
    @Value("${spring.data.mongodb.username}")
    private String auth;
    @Value("${spring.data.mongodb.port}")
    private String port;

    @Override
    public MongoClient mongoClient() {
        MongoClient mongoClient = new MongoClient();
        return mongoClient;
    }

    @Override
    protected String getDatabaseName() {
        return dbName;
    }

    @Override
    @Bean
    public MappingMongoConverter mappingMongoConverter() throws Exception {
        DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.dbFactory());
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext());
        List<Object> list = new ArrayList<>();
        list.add(new BigDecimalToDecimal128Converter());
        list.add(new Decimal128ToBigDecimalConverter());
        converter.setCustomConversions(new MongoCustomConversions(list));
        return converter;
    }

    @Bean
    public MongoDbFactory dbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClientURI("mongodb://"+url+":"+port+"/"+dbName));
    }
    @Override
    @Bean
    public MongoMappingContext mongoMappingContext() {
        MongoMappingContext mappingContext = new MongoMappingContext();
        return mappingContext;
    }
    @Override
    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(this.dbFactory(), this.mappingMongoConverter());
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
